package cn.exrick.sso.service.impl;


import cn.exrick.common.jedis.JedisClient;
import cn.exrick.common.pojo.Result;
import cn.exrick.common.utils.ResultUtil;
import cn.exrick.manager.dto.front.Member;
import cn.exrick.manager.mapper.TbMemberMapper;
import cn.exrick.manager.pojo.TbMember;
import cn.exrick.sso.service.LoginService;
import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;


@Service
public class LoginServiceImpl implements LoginService {

	@Autowired
	private TbMemberMapper tbMemberMapper;
	@Value("${IMAGE_ACCESS_URL}")
	private String imageUrl;
	
	@Override
	public Result<TbMember> userLogin(String username, String password) {

		TbMember tbMember = tbMemberMapper.selectByUserNameOrEmail(username);
		if (tbMember == null) {
			return new ResultUtil<TbMember>().setErrorMsg("用户名或密码错误");
		}
		//md5加密
		if (!DigestUtils.md5DigestAsHex(password.getBytes()).equals(tbMember.getPassword())) {
			return new ResultUtil<TbMember>().setErrorMsg("用户名或密码错误");
		}

		if(StringUtils.isNotBlank(tbMember.getImage())){
			tbMember.setImage(imageUrl + tbMember.getImage());
		}

		return new ResultUtil<TbMember>().setData(tbMember);
	}

}
